package org.dong.control;


import org.dong.pojo.*;
import org.dong.pojo.qo.OrderQo;
import org.dong.pojo.qo.OrderReportQo;
import org.dong.pojo.qo.ReportQo;
import org.dong.service.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderControl {

    @Resource
    OrderService orderService;

    @Resource
    UserService userService;

    @Resource
    ProjectService projectService;

    @Resource
    ReportService reportService;

    @Resource
    TargetService targetService;

    @Resource
    SubjectService subjectService;

    @PostMapping("/Order/getOrderList")
    public Dto getOrderList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Dto dto = new Dto();


        String sDate = req.getParameter("sdate");
        String eDate = req.getParameter("edate");
        Integer userId = ((User)req.getSession().getAttribute("user")).getId();
        String sMeal = req.getParameter("smeal");
        int page = Integer.parseInt(req.getParameter("page"))-1;

        if(page < 0){
            page =  0;
        }else{
            page = page *5;
        }
        if(userId > 0){
             List<Order> orderList = orderService.getOrderList(sDate,eDate,userId,sMeal,page);
            Integer orderListCount = orderService.getOrderListCount(sDate, eDate, userId,sMeal);
            Map<String, Object> datas = new HashMap<String, Object>();
            datas.put("order", orderList);
            datas.put("count",orderListCount);
            dto.setDatas(datas);
            dto.setErrMsg("订单列表");
            dto.setStatusCode(200);
        }else{
            dto.setErrMsg("您还未登录，请先登录");
            dto.setStatusCode(2001);
        }


        return dto;
    }

    @PostMapping("/Order/getOrderInfo")
    public Dto getOrderInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Dto dto = new Dto();

        int id = Integer.parseInt(req.getParameter("id"));

        //先查询订单信息
        Order orderInfo = orderService.getOrderInfo(id,null);
        if(orderInfo != null){
            //查询用户信息
            User user = userService.getUser(null, orderInfo.getUserId(),null);
            //查询套餐对应的体检项目
           List<Project> projectList = projectService.getMealProject(orderInfo.getMealId(),null);
            System.out.println("项目集合"+projectList);
            List<ReportQo> reportQos = new ArrayList<>();
            //根据套餐查询对应的项目报告
            for (Project project : projectList) {
                //查询对应的项目的报告
                Report reportInfo = reportService.getReportInfo(null, orderInfo.getId(), null, project.getId());

                if(reportInfo != null ){
                    //创建项目Qo对象存放当前项目信息和细集合
                    ReportQo reportQo = new ReportQo();

                    reportQo.setProjectName(project.getProjectName());
                    reportQo.setDoctorName(reportInfo.getDoctorName());
                    reportQo.setSubjectName(subjectService.getSubjectName(reportInfo.getSubjectId()));
                    reportQo.setSubjectContent(reportInfo.getSubjectContent());
                    reportQo.setStatus(reportInfo.getStatus());

                    //查询细项结果
                    List<Target> targetList = targetService.getDetailList(reportInfo.getId());
//                    System.out.println("细项列表"+targetList);
                    if(targetList.size() > 0){
                        reportQo.setTargets(targetList);
                    }

                    reportQos.add(reportQo);
                }else{
                    //创建项目Qo对象存放当前项目信息和细集合
                    ReportQo reportQo = new ReportQo();
                    reportQo.setProjectName(project.getProjectName());
                    reportQo.setSubjectName(subjectService.getSubjectName(project.getProjectSubject()));
                    reportQo.setStatus(0);

                    reportQos.add(reportQo);
                }
            }

                //创建一个报告单对象
                OrderReportQo order = new OrderReportQo();
                order.setCardId(user.getCardId());
                order.setRealName(user.getRealName());
                order.setOrderName(orderInfo.getOrderName());
                order.setOrderSn(orderInfo.getOrderSn());
                order.setCreateTime(orderInfo.getCreateTime());
                order.setProjects(reportQos);
                order.setId(orderInfo.getId());
                order.setReportContent(orderInfo.getReportContent());
                order.setStatus(orderInfo.getStatus());


                Map<String, Object> datas = new HashMap<String, Object>();
                datas.put("order", order);
                dto.setDatas(datas);
                dto.setErrMsg("订单详情");
                dto.setStatusCode(200);


        }else{
            dto.setErrMsg("没有找到订单");
            dto.setStatusCode(201);
        }
//        System.out.println(dto);
        return dto;
    }
    @PostMapping("/Order/updateOrderInfoContent")
    public Dto updateOrderInfoContent(HttpServletRequest req){
        Dto dto = new Dto();
        System.out.println(req.getParameter("id"));
        Integer id = Integer.valueOf(req.getParameter("id"));
        Integer status = Integer.valueOf(req.getParameter("status"));
        String content = req.getParameter("content");
        System.out.println(content);
        final Integer integer = orderService.updateOrderInfoContent(id, status, content);

        if(integer > 0){
            dto.setErrMsg("更新成功");
            dto.setStatusCode(200);
        }else{
            dto.setErrMsg("更新失败");
            dto.setStatusCode(2001);
        }

        return dto;
    }
}
